home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ClasspathEntry.java < prev    next >
Text File  |  1998-11-03  |  5KB  |  230 lines

  1. package com.symantec.itools.lang;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.io.File;
  6. import java.util.Enumeration;
  7. import java.util.Hashtable;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipFile;
  10. import com.symantec.itools.io.Directory;
  11. import com.symantec.itools.io.FileSystem;
  12.  
  13.  
  14. /**
  15.  * @author Symantec Internet Tools Division
  16.  * @version 1.0
  17.  * @since VCafe 3.0
  18.  */
  19.  
  20. public class ClasspathEntry
  21. {
  22.     /**
  23.      * @since VCafe 3.0
  24.      */
  25.     protected boolean isFile;
  26.  
  27.     /**
  28.      * @since VCafe 3.0
  29.      */
  30.     protected String name;
  31.     
  32.     private ZipFile zipFileHandle;
  33.     
  34.     public ClasspathEntry(String entry)
  35.     {
  36.         File file;
  37.  
  38.         file   = new File(entry);
  39.         isFile = file.isFile();
  40.  
  41.         if(file.exists())
  42.         {
  43.             name = FileSystem.getCanonicalPath(file, true);
  44.         }
  45.         else
  46.         {
  47.             name = file.getName();
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * @since VCafe 3.0
  53.      */
  54.  
  55.     public String getName()
  56.     {
  57.         return (name);
  58.     }
  59.  
  60.     /**
  61.      * @since VCafe 3.0
  62.      */
  63.  
  64.     public boolean isFile()
  65.     {
  66.         return (isFile);
  67.     }
  68.  
  69.     /**
  70.      * @since VCafe 3.0
  71.      */
  72.  
  73.     public boolean isDirectory()
  74.     {
  75.         return (!(isFile));
  76.     }
  77.  
  78.     /**
  79.      * @param obj TODO
  80.      * @since VCafe 3.0
  81.      */
  82.  
  83.     public boolean equals(Object obj)
  84.     {
  85.         if(obj instanceof ClasspathEntry)
  86.         {
  87.             return (((ClasspathEntry)obj).name.equals(name));
  88.         }
  89.  
  90.         return (false);
  91.     }
  92.  
  93.     /**
  94.      * @since VCafe 3.0
  95.      */
  96.  
  97.     public int hashCode()
  98.     {
  99.         return (name.hashCode());
  100.     }
  101.  
  102.     /**
  103.      * @since VCafe 3.0
  104.      */
  105.  
  106.     public String toString()
  107.     {
  108.         return (name + " " + ((isFile) ? "file" : "directory"));
  109.     }
  110.  
  111.     /**
  112.      * @param classpath TODO
  113.      * @since VCafe 3.0
  114.      */
  115.  
  116.     public Hashtable load(Classpath classpath)
  117.     {
  118.         Hashtable types;
  119.  
  120.         types = new Hashtable();
  121.  
  122.         try
  123.         {
  124.             String[] files;
  125.  
  126.             if(isFile)
  127.             {
  128.                 ZipFile zipFile;
  129.  
  130.                 zipFile = new ZipFile(name);
  131.  
  132.                 for(Enumeration e = zipFile.entries(); e.hasMoreElements();)
  133.                 {
  134.                     ZipEntry entry;
  135.                         
  136.                     entry = (ZipEntry)e.nextElement();
  137.  
  138.                     if(!(entry.isDirectory()))
  139.                     {
  140.                         types.put(entry.getName(), this);
  141.                     }
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 Directory directory;
  147.  
  148.                 directory = new Directory(name);
  149.                 files     = directory.listFiles(true);
  150.  
  151.                 for(int i = 0; i < files.length; i++)
  152.                 {
  153.                     // FIXME - need to load from directories
  154.                     /*
  155.                     try
  156.                     {
  157.                         types.put(classpath.convertResourceNameToJavaName(files[i], true).getFullName(), this);
  158.                     }
  159.                     catch(NotJavaNameException ex)
  160.                     {
  161.                         ex.printStackTrace();
  162.                     }
  163.                     */
  164.                 }
  165.             }
  166.         }
  167.         catch(IOException ex)
  168.         {
  169.         }
  170.  
  171.         return (types);
  172.     }
  173.  
  174.     /**
  175.      * @param entryName TODO
  176.      * @since VCafe 3.0
  177.      */
  178.  
  179.     public boolean find(String entryName)
  180.     {
  181.         if(isFile)
  182.         {
  183.             try
  184.             {
  185.                 return (getZipFile(name).getEntry(entryName) != null);
  186.             }
  187.             catch(IOException ex)
  188.             {
  189.                 return (false);
  190.             }
  191.         }
  192.  
  193.         return (new File(FileSystem.getCanonicalPath(name, true) + entryName.replace('/', File.separatorChar)).exists());
  194.     }
  195.     
  196.     protected ZipFile getZipFile(String name)
  197.         throws IOException
  198.     {
  199.         if(zipFileHandle == null)
  200.         {
  201.             zipFileHandle = new ZipFile(name);
  202.         }
  203.         
  204.         return (zipFileHandle);
  205.     }
  206.     
  207.     public void cleanup()
  208.     {
  209.         if(zipFileHandle != null)
  210.         {
  211.             try
  212.             {
  213.                 zipFileHandle.close();
  214.             }
  215.             catch(IOException ex)
  216.             {
  217.                 Debug.logException(ex);
  218.             }
  219.             
  220.             zipFileHandle = null;
  221.         }
  222.     }
  223.     
  224.     protected void finalize() 
  225.         throws Throwable
  226.     {
  227.         super.finalize();
  228.         cleanup();
  229.     }    
  230. }